package com.tadpole.music.view.fragment.question;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.TabAdapter;
import com.tadpole.music.bean.question.UniversityBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.question.UniversityIView;
import com.tadpole.music.presenter.question.UniversityPresenter;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.StatusBarUtil;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.fragment.base.BaseFragment;
import com.tadpole.music.view.widget.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * 题库
 * Created By 孙岩
 * on 2020/9/8
 */
public class QuestionFragment extends BaseFragment implements UniversityIView {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;
    //TabLayout
    private TabLayout tabLayout;
    //ViewPager
    private NoScrollViewPager viewPager;
    private TabAdapter tabAdapter;
    //tab集合
    private List<String> tabList = new ArrayList<>();
    //viewpager绑定的fragment的集合
    private List<Fragment> fragmentList = new ArrayList<>();
    //获取题库中的院校的presenter
    private UniversityPresenter universityPresenter;


    public static QuestionFragment newInstance(String param1, String param2) {
        QuestionFragment fragment = new QuestionFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_question;
    }

    @Override
    protected void initAllMembersView(Bundle savedInstanceState) {
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected void initView(View mRootView) {
        tabLayout = mRootView.findViewById(R.id.tabLayout);
        viewPager = mRootView.findViewById(R.id.viewPager);
        //将头部状态栏置为透明
        StatusBarUtil.setTranslucentForImageViewInFragment(getActivity(), 0, null);

        universityPresenter = new UniversityPresenter();
        universityPresenter.attachView(this);
        universityPresenter.getUniversity();

    }

    @Override
    public void showUniversity(List<UniversityBean.DataBean> list) {
        initTabData(list);
    }

    @Override
    public void show401() {
        Intent intent = new Intent(getActivity(), LoginActivity.class);

        startActivity(intent);
        SpUtil.getInstance(getActivity()).putString(Constant.TOKEN, "");
    }

    //初始化tab
    private void initTabData(List<UniversityBean.DataBean> list) {
        for(int i=0;i<list.size();i++){
            tabList.add(list.get(i).getName());

            fragmentList.add(CentralMusicFragment.newInstance(list.get(i),""));
        }


        tabAdapter = new TabAdapter(fragmentList, tabList, getChildFragmentManager(), getActivity());
        viewPager.setAdapter(tabAdapter);
        viewPager.setCurrentItem(0);
        tabLayout.setupWithViewPager(viewPager);


        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(tabAdapter.getTabView(i));
            }

            if (i == 0) {
                //设置默认进来第一个是选中状态
                TabLayout.Tab a = tabLayout.getTabAt(0);
                View view1 = a.getCustomView();
                if (null != view1 && view1 instanceof TextView) {
                    ((TextView) view1).setTextSize(16);
                    ((TextView) view1).setTextColor(getResources().getColor(R.color.color_33));
                }
            } else {
                //设置默认进来第二个是不选中状态
                TabLayout.Tab b = tabLayout.getTabAt(1);
                View view2 = b.getCustomView();
                if (null != view2 && view2 instanceof TextView) {
                    ((TextView) view2).setTextSize(13);
                    ((TextView) view2).setTextColor(getResources().getColor(R.color.color_65));
                }
            }
        }


        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        //切换导航菜单时候的监听
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition(), true);
                View view = tab.getCustomView();
                if (null != view && view instanceof TextView) {
                    ((TextView) view).setTextSize(16);
                    ((TextView) view).setTextColor(getResources().getColor(R.color.color_33));
                    ((TextView) view).setTextAppearance(getActivity(), R.style.TabLayoutTextStyle);
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null != view && view instanceof TextView) {
                    ((TextView) view).setTextSize(13);
                    ((TextView) view).setTextColor(getResources().getColor(R.color.color_65));
                }
            }


            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
